package com.juku2024.juku.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import android.text.method.LinkMovementMethod
import android.text.method.ScrollingMovementMethod
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.juku2024.juku.R
import com.juku2024.juku.callback.OnNoDoubleClickListener
import com.juku2024.juku.tools.AppUtils
import com.juku2024.juku.tools.RxUtils
import io.reactivex.disposables.Disposable

/**
 * 通知框
 */
class PermissionDialog(context: Context) : Dialog(context, R.style.CommonDialogStyle) {

    private lateinit var tvTitle: TextView
    private lateinit var tvContent: TextView
    private lateinit var btnCancel: TextView
    private lateinit var btnAlwaysShow: TextView

    private var isInit = false
    private var title: String? = null
    private var txt: String? = null
    private var confirmTxt: String? = null
    private var cancelTxt: String? = null
    private var isShowWaitTime = false
    private var confirmClick: View.OnClickListener? = null
    private var cancelClick: View.OnClickListener? = null
    // 倒计时
    private var disposable: Disposable? = null

    companion object {
        fun init(context: Context,
                 title: String,
                 content: String, btnConfirmText: String, confirmClick: View.OnClickListener,
                 btnCancelTxt: String, cancelClick: View.OnClickListener): PermissionDialog{
            val requestPermissionDialog = PermissionDialog(context)
            requestPermissionDialog.showNotification(
                title,
                content,
                btnConfirmText,
                confirmClick,
                btnCancelTxt,
                cancelClick)
            return requestPermissionDialog
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window?.decorView?.setPadding(0, 0, 0, 0)
        val params = window?.attributes
        val width = context.resources.getDimensionPixelSize(R.dimen.d_300)
        params?.width = width
        params?.height = ViewGroup.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        setContentView(R.layout.dialog_permission)
        // 强升不能取消弹窗
        setCancelable(false)
        // 点击外部区域不能取消对话框
        setCanceledOnTouchOutside(false)
        btnCancel = findViewById(R.id.btn_permission_cancel)
        btnAlwaysShow = findViewById(R.id.btn_permission_confirm)
        tvTitle = findViewById(R.id.tv_permission_NoticeTitle)
        tvContent = findViewById(R.id.tv_permission_NoticeContent)
        // 设置可滑动
        tvContent.movementMethod = ScrollingMovementMethod.getInstance()
        // 设置链接
        tvContent.movementMethod = LinkMovementMethod.getInstance()
        isInit = true
        if (TextUtils.isEmpty(title)) return
        setData()
    }


    fun showNotification(title: String, txt: String, confirmTxt: String, confirmClick: View.OnClickListener,
                         cancelTxt: String? = null, cancelClick: View.OnClickListener? = null, isShowWaitTime: Boolean = false) {
        this.title = title
        this.txt = txt
        this.confirmTxt = confirmTxt
        this.cancelTxt = cancelTxt
        this.confirmClick = confirmClick
        this.cancelClick = cancelClick
        this.isShowWaitTime = isShowWaitTime
        if (!isInit) {
            if (!isShowing) show()
            return
        }
        setData()
        if (!isShowing) show()
    }

    private fun setData(){
        // 设置标题
        tvTitle.text = title
        btnAlwaysShow.text = confirmTxt
        // 加载富文本内容
//        if (!TextUtils.isEmpty(txt)) {
//            tvContent.text = if (Build.VERSION.SDK_INT >= 24)
//                Html.fromHtml(txt, Html.FROM_HTML_MODE_COMPACT)
//            else
//                Html.fromHtml(txt)
//        }
        tvContent.text = txt
        // 判断是否需要倒计时
        btnAlwaysShow.setBackgroundResource(R.drawable.shape_stroke_w6_cccccc_r24)
        btnAlwaysShow.setTextColor(AppUtils.getColor(R.color.cCCCCCC))
        if (isShowWaitTime) {
            val waitTime = 2
            btnAlwaysShow.text =  String.format("%s(%ds)", confirmTxt, waitTime)
            disposable = RxUtils.interval(0, waitTime * 1L + 1, 0, 1000L) {
                btnAlwaysShow.text = String.format("%s(%ds)", confirmTxt, (waitTime - it))
                if (it >= waitTime) {
                    isShowWaitTime = false
                    btnAlwaysShow.text = confirmTxt
                    btnAlwaysShow.setTextColor(AppUtils.getColor(R.color.white))
                    btnAlwaysShow.setBackgroundResource(R.drawable.shape_solid_4488f0_r24)
                }
            }
        } else {
            btnAlwaysShow.setTextColor(AppUtils.getColor(R.color.white))
            btnAlwaysShow.setBackgroundResource(R.drawable.shape_solid_4488f0_r24)
        }

        btnAlwaysShow.setOnClickListener(object : OnNoDoubleClickListener {
            override fun onNoDoubleClick(v: View?) {
                super.onNoDoubleClick(v)
                if (isShowWaitTime) return
                confirmClick?.onClick(v)
                dismiss()
            }
        })

       if (TextUtils.isEmpty(cancelTxt) || cancelClick == null) {
           btnCancel.visibility = View.GONE
        } else {
           btnCancel.visibility = View.VISIBLE
           btnCancel.text = cancelTxt
           btnCancel.setOnClickListener(object : OnNoDoubleClickListener {
               override fun onNoDoubleClick(v: View?) {
                   super.onNoDoubleClick(v)
                   disposable?.dispose()
                   cancelClick?.onClick(v)
                   dismiss()
               }
           })
        }
    }
}